package com.assignwork.service.impl;

import com.assignwork.dto.WorkLineLogDTO;
import com.assignwork.dto.WorkLineLogInfoDTO;
import com.assignwork.dto.WorkLineLogMachineDTO;
import com.assignwork.entity.WorkLineLogInfo;
import com.assignwork.entity.WorkLineLogMachine;
import com.assignwork.service.IWorkLineLogInfoService;
import com.assignwork.service.IWorkLineLogMachineService;
import com.assignwork.vo.WorkLineLogInfoVO;
import com.assignwork.vo.WorkLineLogMachineVO;
import com.assignwork.vo.WorkLineLogVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.common.cache.EsignCache;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;
import org.springblade.core.mp.base.BaseServiceImpl;
import com.assignwork.entity.WorkLineLog;
import com.assignwork.mapper.WorkLineLogMapper;
import com.assignwork.service.IWorkLineLogService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (WorkLineLog)表服务实现类
 */
@AllArgsConstructor 
@Service("workLineLogService")
public class WorkLineLogServiceImpl extends BaseServiceImpl<WorkLineLogMapper, WorkLineLog> implements IWorkLineLogService {

    IWorkLineLogMachineService machineService;
    IWorkLineLogInfoService infoService;

    @Override
    public boolean saveOrUpdateWorkLineLog(WorkLineLogDTO dto) {
        if (dto.getId() != null) {
            this.deleteMachine(dto.getId(), dto.getMachine());
            this.deleteInfo(dto.getId(), dto.getInfo());
        }
        this.saveOrUpdate(dto);
        machineService.saveOrUpdateDto(dto.getMachine());
        infoService.saveOrUpdateDto(dto.getInfo());
        return true;
    }

    private boolean deleteMachine(Long logId, List<WorkLineLogMachineDTO> ls) {
        List<WorkLineLogMachine> list = this.machineService
                .lambdaQuery()
                .eq(WorkLineLogMachine::getLogId, logId)
                .list();
        Set<Long> collect = list.stream().map(WorkLineLogMachine::getId).collect(Collectors.toSet());

        for (WorkLineLogMachineDTO l : ls) {
            if (l.getId() != null) {
                collect.remove(l.getId());
            }
        }

        return machineService.removeByIds(collect);
    }

    private boolean deleteInfo(Long logId, List<WorkLineLogInfoDTO> ls) {
        List<WorkLineLogInfo> list = this.infoService
                .lambdaQuery()
                .eq(WorkLineLogInfo::getLogId, logId)
                .list();

        Set<Long> collect = list.stream().map(WorkLineLogInfo::getId).collect(Collectors.toSet());

        for (WorkLineLogInfoDTO l : ls) {
            if (l.getId() != null) {
                collect.remove(l.getId());
            }
        }

        return infoService.removeByIds(collect);
    }

    @Override
    public WorkLineLogVO workLineLogByArrangeId(Long arrangeId, String workType) {
        WorkLineLog log = this.lambdaQuery()
                .eq(WorkLineLog::getArrangeId, arrangeId)
                .orderByDesc(WorkLineLog::getUpdateTime)
                .last("limit 1")
                .one();

        if (log == null) {
            return null;
        }

        WorkLineLogVO vo = Func.copy(log, WorkLineLogVO.class);

        String sign = EsignCache.getSignByUserId(log.getUpdateUser());
        vo.setSign(sign);

        List<WorkLineLogInfoVO> infoVO = infoService.listVoByArrangeId(arrangeId);
        vo.setInfo(infoVO);

        List<WorkLineLogMachineVO> machineVO = machineService.listVoByArrangeId(arrangeId, workType);
        for (WorkLineLogMachineVO workLineLogMachineVO : machineVO) {
            workLineLogMachineVO.setMachineKindName(DictBizCache.getValue("machine_kind", workLineLogMachineVO.getMachineKind()));
        }
        vo.setMachine(WorkLineLogMachineVO.list2Map(machineVO));

        return vo;
    }

    @Override
    public boolean auditWorkLineLog(WorkLineLogDTO dto) {
        LambdaUpdateWrapper<WorkLineLog> eq = new UpdateWrapper<WorkLineLog>()
                .lambda()
                .set(WorkLineLog::getAuditStatus, dto.getAuditStatus())
                .eq(WorkLineLog::getId, dto.getId());

        return this.update(eq);
    }


}
